package com.xm.servlet.background.commodity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 删除图片
 * @author 康文程
 * @date 2024-1-16 下午 01:02
 */
@WebServlet("/background/delete/commodity/image")
public class DeleteImageServlet extends HttpServlet {
    /**
     * 路径
     */
    private String savePath;

    @Override
    public void init() throws ServletException {
        super.init();
        // 从配置文件中读取图片保存路径
        Properties properties = new Properties();
        InputStream inputStream = getClass().getResourceAsStream("/config.properties");
        try {
            properties.load(inputStream);
            savePath = properties.getProperty("imageSavePath", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String filePath = savePath+req.getParameter("imagName");
        System.out.println(filePath);
        File file = new File(filePath);
        // 检查文件是否存在
        if (file.exists()) {
            resp.getWriter().print(file.delete());
            // 执行删除操作并返回删除结果
        }

    }
}
